package com.zhengjiang.nio;

import java.io.*;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * @author zhengjiang
 * @version 1.0.0
 * @description
 * @date 2021/6/5
 **/
public class FileChannelTest {
    public static void main(String[] args) {
        try {
            File file = new File("E:\\ideal_projects\\zhengjiang-code\\zhengjiang-crazy-java-demo\\src\\main\\java\\com\\zhengjiang\\nio\\FileChannelTest.java");
            FileChannel inChannel = new FileInputStream(file).getChannel();
            FileChannel outChannel = new FileOutputStream("E:\\ideal_projects\\zhengjiang-code\\zhengjiang-crazy-java-demo\\src\\main\\java\\com\\zhengjiang\\nio\\1.txt").getChannel();
            MappedByteBuffer mappedByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            outChannel.write(mappedByteBuffer);

            mappedByteBuffer.clear();
            Charset charset = Charset.forName("GBK");
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer charBuffer = decoder.decode(mappedByteBuffer);
            System.out.println(charBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
